package com.iwdnb.gkgz.application.kline;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.iwdnb.gkgz.application.service.StockRangeService;
import com.iwdnb.gkgz.common.model.dto.StockDayData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 波段低价
 */
@Component
@Slf4j
public class LowRangePrice implements Shape {

    @Autowired
    private StockRangeService stockRangeService;

    @Override
    public List<StockDayData> getMatchData(String code, List<StockDayData> list, List<String> matchDateList) {
        List<StockDayData> stockDayDataList = new ArrayList<>();
        List<String> dateList = stockRangeService.queryLowPriceDateList(code);
        for (int i = 0; i < list.size(); i++) {
            StockDayData stockDayData = list.get(i);
            //if (CollectionUtils.isNotEmpty(matchDateList) && !inMatchDate(stockDayData, matchDateList)) {
            //    continue;
            //}
            //if (i % 100 == 0) {
            //    log.info("LowRangePrice getMatchData working,code:{},index:{}", code, i);
            //}
            if (dateList.contains(stockDayData.getDate())) {
                stockDayDataList.add(stockDayData);
            }

        }
        return stockDayDataList;
    }

    private boolean inMatchDate(StockDayData stockDayData, List<String> matchDateList) {
        Date stockDate = DateUtil.parseDate(stockDayData.getDate());
        for (String d : matchDateList) {
            Date date = DateUtil.parseDate(d);
            Long time = Math.abs(date.getTime() - stockDate.getTime());
            //2个买点间隔2天之内
            if (time <= 2 * 86400 * 1000) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean match(String code, StockDayData data, List<StockDayData> beforeDatas) {
        List<String> dateList = stockRangeService.queryLowPriceDateList(code);
        return dateList.contains(data.getDate());
    }

}